package com.news.news.web;

import com.github.pagehelper.PageInfo;
import com.news.news.modle.Admin;
import com.news.news.modle.Menu;
import com.news.news.modle.Role;
import com.news.news.service.AdminService;
import com.news.news.service.LoginService;
import com.news.news.util.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@RestController
@RequestMapping(path = "/admin/AdminCtl")
public class AdminController {
    @Resource
    AdminService adminService;

    /**
     * 获取列表
     *
     * @param admin
     * @return
     */
    @RequestMapping(value = "/list")
    public CommonResult list(@RequestBody Admin admin) {
//        System.out.println(admin);
        PageInfo<Admin> adminPageInfo = adminService.adminList(admin);
        CommonResult commonResult = new CommonResult(200, adminPageInfo, "成功");

        return commonResult;
    }

    /**
     * 获取新增的角色列表
     *
     * @return
     */
    @RequestMapping(value = "/findRoles")
    public CommonResult findRoles() {
        List<Role> roles = adminService.findRoles();
        CommonResult commonResult = new CommonResult(200, roles, "成功");
        return commonResult;
    }

    /**
     * 修改与新增方法
     *
     * @param admin
     * @param token
     * @return
     */
    @RequestMapping(value = "/saveAdmin")
    public CommonResult saveAdmin(@RequestBody Admin admin, @RequestHeader("adminToken") String token) {
        admin.setAdminToken(token);
        if (admin.getId() != null) {//【修改】
            adminService.updateAdmin(admin);
        } else {//【新增】
            adminService.saveAdmin(admin);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");
        return commonResult;
    }

    /**
     * 编辑查找信息--通过ID
     * @param id
     * @return
     */
    @RequestMapping(value = "/findAdminById")
    public CommonResult findAdminById(Integer id) {
        Admin admin = adminService.findAdminById(id);
        CommonResult commonResult = new CommonResult(200, admin, "成功");
        return commonResult;
    }
    /**
     * 删除管理员
     *
     * @return
     */
    @RequestMapping(value = "/deleteAdmin")
    public CommonResult deleteAdmin(Integer id) {
         adminService.deleteAdmin(id);
        CommonResult commonResult = new CommonResult(200, null, "成功");
        return commonResult;
    }

    /**
     * 循环菜单
     *
     * @return
     */
    @RequestMapping(value = "/showMenu")
    public CommonResult showMenu(@RequestHeader(value = "adminToken")String adminToken) {
     ArrayList<Menu> menus= adminService.showMenu(adminToken);
        CommonResult commonResult = new CommonResult(200,menus , "成功");
        return commonResult;
    }
}
